<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\admin\model\Banner;
use app\admin\model\Indexmodel;
use app\admin\model\News;
use app\admin\model\Message;
use think\Db;


class Index extends Frontend
{
    public $title = [
                    'index'=>'首页',
                    'product'=>'硬件产品',
                    'consumer'=>'消费者体验',
                    'business'=>'高效运营',
                    'aboutus'=>'关于我们',
                    'partner'=>'生态合作伙伴',
                    'practice'=>'最佳实践',
                    'practice_info'=>'详情',
                    'hudong'=>'详情',
                    'platform'=>'SEMO开放平台',
                    'property'=>'资产管理',
                    'newsinfo'=>'新闻详情',
                    ];
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
 	public function _initialize()
    {
        parent::_initialize();
        $this->view->engine->layout(true);

        //产品下拉
        $where['pid'] = ['neq',0];
        $where['switch'] = ['eq',1];
        $this->products = Db::name('products')->where($where)->order('weigh desc')->select();
        
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        $this->assign('products',$this->products);

        //footer
        $footers = Db::name('footer')->where(['pid'=>0])->order('weigh')->select();
        foreach($footers as $k=>$v){
            $footers[$k]['child'] = Db::name('footer')->where(['pid'=>$v['id']])->order('weigh')->select();
        }
        
        $this->assign('titles',$this->title[request()->action()]);
        $this->assign('footers',$footers);
    }
    //首页
    public function index()
    {
        
        //轮播
        $banner = Db::table('se_banner')->where(['switch'=>1])->select();
        //新闻
        $news = Db::table('se_news')->where(['switch'=>1])->order('weigh')->select();


        //三大解决方案
        $indexmodel1 = Indexmodel::get(['id'=>1]);
        $indexmodel2 = Indexmodel::get(['id'=>2]);
        $indexmodel3 = Indexmodel::get(['id'=>3]);

        $this->assign('banner',$banner);
        $this->assign('indexmodel1',$indexmodel1);
        $this->assign('indexmodel2',$indexmodel2);
        $this->assign('indexmodel3',$indexmodel3);
        $this->assign('news',$news);
        
        $this->view->engine->layout(false);

        return $this->view->fetch();
    }
    
    //硬件产品
    public function product(){
    	$banner = Db::name('productbanner')->where(['switch'=>1])->find();
        $data = Db::name('products')->where(['pid'=>0,'switch'=>1])->order('weigh desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['child'] = Db::name('products')->where(['pid'=>$v['id']])->order('weigh desc')->select();
        }
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }

    //消费者体验
    public function consumer(){
        $banner = Db::name('consumerbanner')->where(['switch'=>1])->find();
        $data = Db::name('consumer')->where(['switch'=>1])->order('weigh desc')->select();
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }

    //高效运营
    public function business(){
        $banner = Db::name('businessbanner')->where(['switch'=>1])->find();
        $data = Db::name('business')->where(['switch'=>1])->order('weigh desc')->select();
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }

    //关于我们
    public function aboutus(){
        //公司简介
        $about = Db::name('aboutus')->find();
        //新闻专栏
        $news = Db::name('news')->where(['switch'=>1])->order('weigh desc')->select();
        $recruit = Db::name('recruit')->where(['switch'=>1])->order('weigh desc')->select();

        $this->assign('about',$about);
        $this->assign('news',$news);
        $this->assign('recruit',$recruit);

        return $this->view->fetch();
    }
    //建议消息
    public function message(){
        
        $message= new Message();
        $message->data([
                'name'=>input('name'),
                'phone'=>input('phone'),
                'mail'=>input('e-mail'),
                'content'=>input('content'),
                'date'=>date('Y-m-d H:i:s'),
            ]);
         // $validate = \think\Loader::validate('Message');
         // if($validate->check($message)){ //注意，在模型数据操作的情况下，验证字段的方式，直接传入对象即可验证
                $db= $message->save();  //这里的save()执行的是添加
                if($db){
                    return $this->success('提交成功','aboutus');
                }else{
                    return $this->error('提交失败');
                }
            // }else{
            //     return $this->error($validate->getError());
            // }
    }
    //生态合作伙伴
    public function partner(){
        $banner = Db::name('partnerbanner')->where(['switch'=>1])->find();
        $pid = Db::name('partner')->where(['pid'=>0,'switch'=>1])->order('weigh desc')->select();
        $data = Db::name('partner')->where(['pid'=>0,'switch'=>1])->order('weigh desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['child'] = Db::name('partner')->where(['pid'=>$v['id']])->order('weigh')->select();
        }
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);
        $this->assign('pid',$pid);

        return $this->view->fetch();
    }

    // 最佳实践
    public function practice(){
        $banner = Db::name('practicebanner')->where(['switch'=>1])->find();
        $data = Db::name('practice')->where(['switch'=>1])->order('weigh desc')->select();
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }
    //最佳实践详情页
    public function practice_info(){
        $id = $this->request->param()['id'];

        $data = Db::name('practice')->where(['switch'=>1,'id'=>$id])->find();
        // dump($data);die;
        $this->assign('data',$data);
        
        return $this->view->fetch();
    }
    //semo平台
    public function platform(){
        $banner = Db::name('platformbanner')->where(['switch'=>1])->find();
        $data = Db::name('platform')->where(['switch'=>1])->order('weigh desc')->select();
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }
    //互动屏
    public function hudong(){
        $id = $this->request->param()['id'];

        $data = Db::name('products')->where(['switch'=>1,'id'=>$id])->find();
        // $data = Db::name('hudong')->where(['switch'=>1])->order('weigh desc')->select();
        // $vedio = Db::name('hudongvedio')->find();
        // dump($data);die;
        // $this->assign('banner',$banner);
        $this->assign('data',$data);
        // $this->assign('vedio',$vedio);
        
        return $this->view->fetch();
    }

    //资产管理
    public function property(){
        $banner = Db::name('propertybanner')->where(['switch'=>1])->find();
        $data = Db::name('property')->where(['switch'=>1])->order('weigh desc')->select();
        
        $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }

    //新闻详情页
    public function newsinfo(){
        
        $id = $this->request->param()['id'];
        
        $data = Db::name('news')->where(['id'=>$id])->find();
        // dump($data);die;
        // $this->assign('banner',$banner);
        $this->assign('data',$data);

        return $this->view->fetch();
    }

}
